# coding:utf-8
import random
import time
import csv

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains as AC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

import setting

wd = webdriver.Chrome(setting.WEBDRIVERURI)
wd.implicitly_wait(10)

url1 = r'https://top.baidu.com/?fr=mhd_card'
url2 = r'https://www.toutiao.com/search/?keyword='

wd.get(url1)


# 将数据写入csv
def write(item):
    with open(r'jrtt.csv', 'a') as f:
        writer = csv.writer(f)
        try:
            writer.writerow(item)
        except:
            print('error')


# 获取具体信息
def getinfo(url):
    print("当前正在解析页面:", url)
    if wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/div[1]/span[1]').text in ['原创', ]:

        id_ = wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/div[1]/span[2]')
        time_ = wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/div[1]/span[3]')
    else:
        id_ = wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/div[1]/span[1]')
        time_ = wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/div[1]/span[2]')
    title_ = wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/h1')
    text_ = wd.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[1]/article')
    item = [id_.text, time_.text, title_.text, text_.text]
    return item


hw_dict = []
for i in range(10):
    xpath = '//*[@id="hot-list"]/li[' + str(i + 1) + ']/a[1]'
    # print(wd.find_element_by_xpath(xpath).get_attribute('title'))
    value = wd.find_element_by_xpath(xpath).get_attribute('title')
    hw_dict.append(value)

print(hw_dict)

for key in hw_dict:
    time.sleep(random.randint(1, 5))
    url = url2 + key
    wd.get(url)
    xpath = r'//*[@id="J_section_0"]/div/div/div[1]/div/div[1]/a/span'
    try:
        WebDriverWait(wd, 20, 0.5).until(EC.presence_of_element_located((By.XPATH, xpath)))
    finally:
        wd.find_element_by_xpath(xpath).click()
    # print(wd.current_url.split('/'))
    all_handles = wd.window_handles
    wd.switch_to.window(all_handles[-1])

    if wd.current_url.split('/')[2] not in ['www.toutiao.com', ]:
        wd.switch_to.window(all_handles[0])
        continue

    # wd.switch_to.window(all_handles[-1])

    write(getinfo(url))

wd.quit()
